package sdkshell.kuaiyan.com.kuaiyansdk.http.callback;

import android.content.Context;
import android.util.Log;

import com.wmcsk.dl.util.LogUtils;

import org.json.JSONException;

import java.net.SocketTimeoutException;
import sdkshell.kuaiyan.com.kuaiyansdk.http.Net;
import sdkshell.kuaiyan.com.kuaiyansdk.http.exception.NetWorkException;
import sdkshell.kuaiyan.com.kuaiyansdk.http.exception.NoWifiExcetion;
import sdkshell.kuaiyan.com.kuaiyansdk.util.StringUtils;
import sdkshell.kuaiyan.com.kuaiyansdk.util.ToastUtils;
/**
 * Created by Max on 2016/6/23.
 */
public  class HttpCallBack<T> extends ResultCallback<T> {
    protected LogUtils logUtils = LogUtils.getNewInstance(HttpCallBack.class);
    public HttpCallBack(Net net){
        this.net = net;
    }
    public HttpCallBack(){

    }
    public HttpCallBack setNet(Net net){
        this.net = net;
        return this;
    }
    public HttpCallBack setContext(Context context){
        this.context = context;
        return this;
    }

    @Override
    public void onError(Exception e, String response) {
        logUtils.iob("                                                                ");
        logUtils.iob("                                                                ");
        logUtils.iob("                                                                ");
        logUtils.iob("                                                                ");
        logUtils.iob("网络请求开始------------------------------------------------------------------------");
        if(net!=null){
            logUtils.dob("请求url-----》"+(net.getConfig().getNetConfig().getUrl()==null?"未设置url":net.getConfig().getNetConfig().getUrl()));
            if(net.getConfig().getNetConfig().getContent()==null) {
                logUtils.dob("请求参数-----》" + StringUtils.MapToString(net.getConfig().getNetConfig().getParams()));
            }else{
                logUtils.dob("请求参数-----》" + net.getConfig().getNetConfig().getContent());
            }
            logUtils.dob("请求协议--->"+ net.getConfig().getNetConfig().getProtrol());
            logUtils.dob("请求头部--->"+ StringUtils.MapToString(net.getConfig().getNetConfig().getHeads()));
            logUtils.dob("请求类型---》"+net.getConfig().getNetConfig().getType().name());
        }
        if (e instanceof JSONException) {
            if(net!=null) {
                if(context!=null) {
                    if (net.getConfig().getExceptionConfig().isShowGsonExcetionToast()) {
                        ToastUtils.showDefault(context, e == null ? "null" : e.getMessage());
                    } else {
                        onResponse(null, response);
                    }
                }
            }
            logUtils.dob("json 异常" + e == null ? "" : e.getMessage());
            logUtils.dob("错误json："+response);
        }else if(e instanceof NetWorkException) {
            logUtils.dob("网络连接异常" + e == null ? "" : e.getMessage());
            if(context!=null){
                if(net!=null){
                    boolean showToast = net.getConfig().getExceptionConfig().getToastForNoNetwork();
                    if(showToast){
                        ToastUtils.showDefault(context,"网络异常，请检查您的网络连接");
                    }
                }
            }
        }else if(e instanceof NoWifiExcetion){
            logUtils.dob("wifi异常" + e == null ? "" : e.getMessage());
            if(context!=null){
                if(net!=null){
                    boolean showToast = net.getConfig().getExceptionConfig().getToastForNoNetwork();
                    if(showToast){
                        ToastUtils.showDefault(context,"网络异常，请检查您的网络连接");
                    }
                }
            }
        }else if(e instanceof SocketTimeoutException){
            logUtils.dob("超时异常" + e == null ? "" : e.getMessage());
            if(net !=null) {
                int currentTryTime = net.getConfig().getNetConfig().getCurrentTryTime();
                if (currentTryTime < net.getConfig().getNetConfig().getRetry()) {
                    currentTryTime++;
                    net.getConfig().getNetConfig().setCurrentTryTime(currentTryTime);
                    net.requestNet_info(this);
                }
            }
        }else {
            if(net!=null) {
                if(context!=null) {
                    if (net.getConfig().getExceptionConfig().getToastForIoException()) {
                        ToastUtils.showDefault(context, e == null ? "null" : e.getMessage());
                    }
                }
            }
            logUtils.dob("io 异常" + e == null ? "" : e.getMessage());
        }
        logUtils.iob("网络请求结束------------------------------------------------------------------------");
    }

    @Override
    public void onResponse(T response,String json) {
        if(net!=null){
            logUtils.iob("                                                                ");
            logUtils.iob("                                                                ");
            logUtils.iob("                                                                ");
            logUtils.iob("                                                                ");
            logUtils.iob("网络请求开始------------------------------------------------------------------------");
            logUtils.dob("请求url-----》"+net.getConfig().getNetConfig().getUrl());
            if("http://27.30lm.com/appc.php".equals(net.getConfig().getNetConfig().getUrl())){
                logUtils.dob("请求url提交数据");
            }
            if(net.getConfig().getNetConfig().getContent()==null) {
                logUtils.dob("请求参数-----》" + StringUtils.MapToString(net.getConfig().getNetConfig().getParams()));
            }else{
                logUtils.dob("请求参数-----》" + net.getConfig().getNetConfig().getContent());
            }
            logUtils.dob("请求头部--->"+ StringUtils.MapToString(net.getConfig().getNetConfig().getHeads()));
            logUtils.dob("请求协议--->"+ net.getConfig().getNetConfig().getProtrol());
            logUtils.dob("请求类型---》"+net.getConfig().getNetConfig().getType().name());
            if(net.getConfig().getNetConfig().getUrl().contains("stats.php")){
                Log.e("KuanyanSDK","result = "+json);
            }
            logUtils.dob("返回结果json:"+json);
            logUtils.dob("下面是json转换结果：");
            if(response instanceof String){
                logUtils.dob("T=String:"+json);
            }
            logUtils.iob("网络请求结束------------------------------------------------------------------------");
        }
    }
}
